<template>
  <a-modal
    :title="title"
    v-model="show"
    :maskClosable="false"
    :width="1050"
    placement="right"
    :closable="true"
    @cancel="onClose"
    :footer="null"
    :visiable="roleAcctInfoOperationVisiable"
  >
    <a-form :form="form">
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="科目/账号标识"
                       v-bind="formItemLayout">
            <a-select
              show-search
              placeholder="请输入代码或名称"
              option-filter-prop="children"
              style="width: 334px"
              :filter-option="filterOption"
              @focus="selectSettleAcct"
              @search="selectSettleAcct"
              :disabled="this.isUpd"
              v-decorator="['settleAcctId',{rules: [{ required: true, message: '必填字段'}]}]"
            >
              <a-select-option v-for="(d,index) in settleAcctData" :key="d.id">
                {{ d.id }}-{{d.name}}
              </a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item label="id"
                       v-bind="formItemLayout" style="display: none">
            <a-input v-decorator="['id']"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item label="科目/账号描述"
                       v-bind="formItemLayout">
            <a-input v-decorator="['settleAcctDesc',{rules: [{ required: true, message: '必填' }]}]"
                     :disabled="this.isUpd"/>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="所属分支机构"
                       v-bind="formItemLayout">
            <a-select
              show-search
              placeholder="请输入代码或名称"
              option-filter-prop="children"
              style="width: 334px"
              :filter-option="filterOption"
              @focus="selectRole"
              @search="selectRole"
              :disabled="this.isUpd"
              v-decorator="['roleId',{rules: [{ required: true, message: '必填字段'}]}]"
            >
              <a-select-option v-for="(d,index) in roleData" :key="d.id">
                {{ d.id }}-{{d.name}}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item label="实际账户"
                       v-bind="formItemLayout">
            <a-input v-decorator="['actualAcct',{rules: [{ required: true, message: '必填' }]}]"
                     :disabled="this.isUpd"/>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="是否模板用户"
                       v-bind="formItemLayout">
            <a-select
              mode="default"
              :allowClear="true"
              style="width: 100%"
              show-search
              placeholder="请选择"
              option-filter-prop="children"
              :filter-option="filterOption"
              :disabled="this.isUpd"
              v-decorator="['templateAccountFlag',{rules: [{ required: true, message: '必填' }]}]">
              <a-select-option key="Y">是</a-select-option>
              <a-select-option key="N">否</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :md="12" :sm="24" v-show="this.isStop == true">
          <a-form-item label="停用原因"
                       v-bind="formItemLayout">
            <a-input v-decorator="['stopReason',{rules: [{ required: this.isStop, message: '必填' }]}]"/>
          </a-form-item>
        </a-col>

        <a-col :md="12" :sm="24" v-show="this.isCancel == true">
          <a-form-item label="注销原因"
                       v-bind="formItemLayout">
            <a-input v-decorator="['cancelReason',{rules: [{ required: this.isCancel, message: '必填' }]}]"/>
          </a-form-item>
        </a-col>
      </a-row>
      <br>

    </a-form>
    <div class="drawer-bootom-button" v-show="this.isView == true">
      <a-button @click="onClose" type="primary">取消</a-button>
      <a-button @click="handleSubmit" type="primary" :loading="loading">提交</a-button>
    </div>
  </a-modal>
</template>
<script>
  const formItemLayout = {
    labelCol: {span: 8},
    wrapperCol: {span: 16}
  }
  export default {
    name: 'RoleAcctInfoOperation',
    props: {
      roleAcctInfoOperationVisiable: {
        default: false
      },
      instId: '',
    },
    data() {
      return {
        title: '新增',
        isStop: false,
        isCancel: false,
        isUpd: false,
        isView: true,
        loading: false,
        formItemLayout,
        form: this.$form.createForm(this),
        validateStatus: '',
        settleAcctData: [],
        roleData: [],
        info: {
          infoName: '',
          remark: '',
          menuId: ''
        }
      }
    },
    computed: {
      show: {
        get: function () {
          return this.roleAcctInfoOperationVisiable
        },
        set: function () {
        }
      }
    },
    mounted() {

    },

    methods: {
      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },

      selectSettleAcct(val) {
        this.settleAcctData = []

        let param = {}
        if (val) {
          param = {
            q: encodeURIComponent(val),
            pageSize: 20
          }
        } else {
          param = {
            q: '',
            pageSize: 20
          }
        }
        this.$get('admin/csmc/subjectInfo/select2', param).then((r) => {
          let result = r.data.results
          result.forEach(r => {
            this.settleAcctData.push(r);
          })
        })

      },
      selectRole(val) {
        this.roleData = []
        let param = {}
      /*  if (val) {
          param = {
            q: encodeURIComponent(val),
            pageSize: 20
          }
        } else {
          param = {
            q: '',
            pageSize: 20
          }
        }*/
        this.$get('/admin/merParam/UpmpOrganization/organizationSelect2List', {instId : this.info.instId}).then((r) => {
          let result = r.data.rows
          result.forEach(r => {
            this.roleData.push(r);
          })
        })

      },


      reset() {
        this.loading = false
        this.isCancel = false
        this.isUpd = false
        this.isStop = false
        this.title = '新增'
        this.isView = true
        this.form.resetFields()
      },
      onClose() {
        this.reset()
        this.$emit('close')
      },
      handleSubmit() {
        this.form.validateFields((err, values) => {
          if (!err) {
            this.setInfoFields()
            this.loading = true
            this.info.instId = this.instId

            if (this.title == '新增') {
              this.info.optFlag = 'add'
            } else if (this.title == '修改') {
              this.info.optFlag = 'upd'
            } else if (this.title == '停用') {
              this.info.optFlag = 'stop'
            } else if (this.title == '启用') {
              this.info.optFlag = 'use'
            } else if (this.title == '注销') {
              this.info.optFlag = 'cancel'
            }

            if (this.title == '新增') {
              this.$post('/admin/csmc/cRoleAcctInfo', {
                ...this.info
              }).then((r) => {
                this.reset()
                this.$emit('success')
              }).catch(() => {
                this.loading = false
              })
            } else {
              this.$put('/admin/csmc/cRoleAcctInfo', {
                ...this.info
              }).then((r) => {
                this.reset()
                this.$emit('success')
              }).catch(() => {
                this.loading = false
              })
            }

          }
        })
      },

      setInfoFields() {
        let values = this.form.getFieldsValue(['id', 'settleAcctId', 'settleAcctDesc', 'roleId', 'actualAcct',
          'templateAccountFlag', 'stopReason', 'cancelReason'])
        if (typeof values !== 'undefined') {
          Object.keys(values).forEach(_key => {
            this.info[_key] = values[_key]
          })
        }
      },

      setRoleAcctInfoFormValues({...info}) {
        console.log(info.instId)
        this.info.instId = info.instId
        let fields = ['id', 'settleAcctId', 'settleAcctDesc', 'roleId', 'actualAcct', 'templateAccountFlag']
        if (info.operation == 'stop') {
          this.isStop = true
          this.isUpd = true
          this.title = '停用'
        } else if (info.operation == 'use') {
          this.isUpd = true
          this.title = '启用'
        } else if (info.operation == 'cancel') {
          this.isCancel = true
          this.isUpd = true
          this.title = '注销'
        } else if (info.operation == 'view') {
          this.isView = false
          this.isUpd = true
          this.title = '详情'
        } else if (info.operation == 'upd') {
          this.title = '修改'
        }
        Object.keys(info).forEach((key) => {
          if (fields.indexOf(key) !== -1) {
            this.form.getFieldDecorator(key)
            let obj = {}
            obj[key] = info[key]
            this.form.setFieldsValue(obj)
          }
        })
      },

    }

  }
</script>




